// 云函数入口文件
const cloud = require('qq-server-sdk');
const nodeExcel = require('excel-export');
const path = require('path');
cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async(event, context) => {
    let act = event.act;
    let list = event.list;
    var tableHead = [];
    for (let i = 0; i < act.length; i++) {
        tableHead.push(act[i]);
    }
    var tableMap = {
            styleXmlFile: path.join(__dirname, "styles.xml"),
            name: Date.now() + "-export",
            cols: [],
            rows: [],
        }
        //添加表头
    for (var i = 0; i < tableHead.length; i++) {
        tableMap.cols[tableMap.cols.length] = {
            caption: tableHead[i],
            type: 'string'
        }
    }
    //添加每一行数据
    for (var i = 0; i < list.length; i++) {
        let output = [];
        for (let j = 0; j < act.length; j++) {
            output.push(list[i][act[j]]);
        }
        tableMap.rows[tableMap.rows.length] = output
    }

    //保存excelResult到相应位置
    var excelResult = nodeExcel.execute(tableMap);
    var filePath = "outputExcels";
    var fileName = event.alliance + '.xlsx';
    return await cloud.uploadFile({
        cloudPath: path.join(filePath, fileName),
        fileContent: new Buffer(excelResult, 'binary')
    });
}